package com.kkb.kkbadmin.controller;


import com.kkb.kkbadmin.domain.ProjectRegister;
import com.kkb.kkbadmin.domain.User;
import com.kkb.kkbadmin.service.ProjectRegisterService;
import com.kkb.kkbcommon.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName ProjectRegisterController
 * @Description 项目报名
 * @Author mzj
 * @Date 2021/8/13
 **/
@RestController
@Api(tags = "项目报名相关API", value = "项目报名信息上传、根据项目ID查询导师/学生已报名人数、审核学生信息、根据用户ID统计用户已完成项目的总数")
@RequestMapping("projectRegister")
public class ProjectRegisterController {
    @Resource
    private ProjectRegisterService service;

    /**
     * 点击报名，发送用户信息到后台
     *
     * @param userId
     * @param projectId
     * @return
     */
    @ApiOperation(value = "项目报名", notes = "点击立即报名，向数据库报名表插入两条信息：userId和projectI")
    @PostMapping(value = "/register")
    public CommonResult<ProjectRegister> register(@RequestParam("userId") long userId, @RequestParam("projectId") long projectId) {
        service.register(userId, projectId);
        return CommonResult.success(null, "报名成功，等待审核！");
    }

    /**
     * 根据项目id查询报名信息
     *
     * @param projectId 项目id
     * @return CommonResult<List < ProjectRegister>>
     */
    @ApiOperation(value = "查询报名信息", notes = "点击项目查询，向后台数据库中查询对应id的项目报名信息")
    @GetMapping(value = "/register/{id}")
    public CommonResult<List<ProjectRegister>> getRegisterProjectInfo(@PathVariable("id") long projectId) {
        List<ProjectRegister> projectRegisters = service.getRegisterProjectInfo(projectId);
        return CommonResult.success(projectRegisters, "查询成功");
    }

    /**
     * 统计用户已完成项目总数
     *
     * @param userId
     * @return CommonResult<Integer>
     */
    @ApiOperation(value = "统计用户已完成项目总数")
    @GetMapping("/count/{id}")
    public CommonResult<Integer> finshed(@PathVariable("id") Long userId) {
        int i = service.completedProjects(userId);
        return CommonResult.success(i);
    }

    /**
     * 学生审核 ：修改学生报名表的状态为已报名
     *
     * @param userList  学生列表
     * @param projectId 项目id
     * @return CommonResult
     */
    @ApiOperation(value = "审核学生", notes = "项目导师审核学生，修改学生报名状态")
    @PutMapping("/stu/{id}")
    public CommonResult auditStu(@RequestBody @Validated List<User> userList, @Validated @PathVariable("id") Long projectId) {
        service.auditStu(userList, projectId);
        return CommonResult.success(null);
    }
}
